Imports System.IO
Public Class TreeViewDemo
    Private Sub GetDisk()
        Dim i As Integer = 0
        ' Duyeät qua töøng teân oå ñóa
        Dim d As String
        For Each d In Directory.GetLogicalDrives()
            ' Theâm Node vaøo ñieàu khieån TreeView
            Dim n As TreeNode = TreeView1.Nodes.Add(d)
            n.Tag = d
            n.Nodes.Add("Nut con ...")
        Next
    End Sub

   

    Private Sub TreeViewDemo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GetDisk()
        TreeView1.ImageList = Me.ImageList1
    End Sub

    
    Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
        If (e.Node.Nodes(0).Text = "Nut con ...") Then
            e.Node.Nodes.Clear()

            For Each FullPath As String In Directory.GetDirectories(e.Node.Tag)
                Dim idx As Integer = FullPath.LastIndexOf("\")
                Dim childNode As TreeNode = e.Node.Nodes.Add(FullPath.Substring(idx + 1))
                childNode.Tag = FullPath
                childNode.Nodes.Add("Nut con ...")

                childNode.SelectedImageIndex = 1
                childNode.ImageIndex = 0

            Next
        End If

        If e.Node.Nodes.Count > 0 AndAlso e.Node.Nodes(0).Text = "Nut con ..." Then
            e.Cancel = True
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TreeView1.SelectedNode.ExpandAll()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TreeView1.CollapseAll()
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

        If Me.ListView1.Columns.Count = 0 Then
            Me.ListView1.Columns.Add("Name", 120, HorizontalAlignment.Left)
            Me.ListView1.Columns.Add("Size", 50, HorizontalAlignment.Right)
            Me.ListView1.Columns.Add("Type", 120, HorizontalAlignment.Right)
        End If
        Me.ListView1.Items.Clear()

        Dim dir As New DirectoryInfo(e.Node.Tag)
        Dim i As Integer = 0

        ListView1.FullRowSelect = True
        ListView1.SmallImageList = Me.ImageList1
      
        ListView1.View = View.Details

        Dim item As ListViewItem

        Dim f As FileInfo
        For Each f In dir.GetFiles("*.*")
            item = New ListViewItem(f.Name)
            i = i + 1
            Dim imgIdx As Integer = i Mod 5
            item.ImageIndex = imgIdx + 2

            item.SubItems.Add(f.Length.ToString())
            item.SubItems.Add(f.Attributes.ToString())
            ListView1.Items.Add(item)
        Next
    End Sub
End Class
